home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Pascal
/
Skel 3.0i
/
Skel setup.p
< prev
next >
Wrap
Text File
|
1994-05-15
|
6KB
|
190 lines
unit Setup;
interface
uses
Globals;
procedure Setup;
implementation
{SetUps for handling memory}
{########################## SetUpMemory ############################}
{ This very important set of initializations can be left out of the}
{ first versions of a program. We are making sure that memory is laid}
{ out as we desire, with adequate protection against running out of}
{ memory, bad handles, etc.}
procedure SetupMemory; {NOT USED}
const
maxStackSize = 8192; (* max size of stack; the heap gets the rest *)
type
lomemptr = Ptr; (* a pointer to low memory locations *)
var
nilptr: lomemptr; (* will have value NIL *)
stackbaseptr: lomemptr; (* points to current stack base *)
begin
{ If you define a GrowZone function to Handle bad memory problems,}
{ you should define it at the top level (not nested), and set it}
{ here. We don't.}
(* SetGrowZone(&mygrowzone); *)
{ Place a longint -1 (an odd and therefore illegal address) in the}
{ memory location that would be referenced by an accidentally-NULL}
{ Handle, so the error will be caught at Handle-reference time (as}
{ an Address error, ID=02) instead of later on.}
{NOTE: This hardy helps any more, at least not the odd address, since that's legal}
{nowadays (on 68020 and better).}
nilptr := nil;
nilptr^ := LongInt(-1);
{ If you needed to use an Application heap limit other than the}
{ default (which allows 8K for the stack), you'd set it here,}
{ possible using this technique of explicitly specifying the maximum}
{ stack size and allocating the rest to the heap. Should be}
{ independent of memory size. }
stackbaseptr := lomemptr($908);
(* CurStackBase from Tlasm/sysequ.text *)
SetApplLimit(Ptr(stackbaseptr^ - maxStackSize));
{ Expand the application heap Zone to its maximum size, without}
{ purging any purgeable resources. This saves memory compactions}
{ and heap expansions later.}
MaxApplZone;
{ get plenty of master pointers now; if we let the Memory Manager}
{ Allocate them as needed, they'd form non-relocatable islands in}
{ the heap.}
MoreMasters;
MoreMasters;
MoreMasters;
{ Here you might install bulwarks against running out of memory}
{ unexpectedly. One such (cheesy) technique is to here Allocate}
{{ a large Handle, call it "CheeseBuf", which you can de-Allocate}
{ in your GrowZone function, when you must obtain more memory to}
{ avoid a crash. While de-allocated, the program could prevent}
{ the user from doing anything requiring memory, and tell him he}
{ must discard windows or some such memory freeing action. Each}
{ time he does so, the program can try to re-Allocate CheeseBuf;}
{ if it succeeds, the user can go on doing memory-eating operations.}
end; (* SetUpMemory *)
{############################ SetUpMenus #############################}
{ Once-only initialization for menus}
{ We read in all menus from the resource file, and install them,}
{ and all desk accessories (drivers).}
procedure SetupMenus;
var
i: Integer;
begin
for i := 1 to lastmenu do (* get all my menus in *)
mymenus[i] := GetMenu(i); (* use the fact that our menu ID's start at 1 *)
{mymenus[applemenu] := NewMenu(applemenu, char(24));}
{AppendMenu(mymenus[applemenu], 'About Skel…;-');}
AddResMenu(mymenus[applemenu], 'DRVR');
(* pull in all desk accessories *)
for i := 1 to lastmenu do
InsertMenu(mymenus[i], 0);
DrawMenuBar;
end;
{body of SetUp}
{Once-only initialization for Skel}
{############################ SetUp ##############################}
{ Initialize our program. It seems best to Handle:}
{ Memory inits first, ToolBox inits second, then the program variables'}
{ inits. Note that the order of inits is important; see "Using the}
{ Dialog Manager" in the Dialog Mgr section.}
procedure Setup;
var
screenrect: Rect;
theWorld: SysEnvRec;
begin
{ The following function is not declared in <win.h>, although}
{ that is where you would expect it. - WHJ 3/10/85}
{SetupMemory unnecessary: Think Pascal initializes things the proper way.}
{You may want to put some of it back in, though - particularly a few MoreMasters}
{setupmemory; (* init memory layout and protection *)
(* init QuickDraw, and everybody else *)
{NOT needed - Think Pascal does it for us}
{InitGraf(thePort);}
{InitFonts;}
{InitWindows;}
{InitMenus;}
{TEInit;}
{InitDialogs(nil);}
{Flush events, but don't flush disk events.}
FlushEvents(everyEvent - diskMask, 0);
{ Init the system event mask, in case the previous program left}
{ it in a bad state. If you set it non-standard here, FIX IT}
{ BEFORE EXITING, because the Finder (1.1g) does NOT set it.}
(* SetEventMask (everyEvent - keyUpMask);*)
(* standard setting *)
{ Get the port which is the whole screen, to use when deactivating}
{ our window. This prevents the current GrafPort pointer from}
{ ever dangling.}
GetWMgrPort(screenport); (* get whole screen port that window mgr uses *)
SetPort(screenport); (* and start off with it *)
{Check some system features /LIR}
gWNEImplemented := NGetTrapAddress($60, ToolTrap) <> NgetTrapAddress($9F, ToolTrap);
if SysEnvirons(1, theWorld) = noErr then
gColorQDFlag := theWorld.hasColorQD;
{ get window: use wRecord storage. Port is set to that of the}
{ new window.}
{ GetNewWindow posts an update event for the new window,}
{ so it will be redrawn right away.}
{Get a color window if CQD is available! /LIR}
if gColorQDFlag then
mywindow := GetNewCWindow(windowid, @wrecord, WindowPtr(-1))
else
mywindow := GetNewWindow(windowid, @wrecord, WindowPtr(-1));
(* -1 => frontmost window *)
(* set up dragRect; we can drag the window within it *)
screenrect := screenBits.bounds; (* don't assume screen size *)
(* set drag Rect to avoid menu bar, and keep at least 4 pixels *)
(* on screen *)
SetRect(dragrect, 4, 24, screenrect.right - 4, screenrect.bottom - 4);
(* set up GrowRect, for limits on window growing *)
SetRect(growrect, 48, 14, screenrect.right - 7, screenrect.bottom - 7);
(* pull in and set up our menus *)
SetupMenus;
{Set some globals that are necessary for the new features /LIR}
gSleep := 5;
gAppleEventsInitialized := false;
{We have to InitCursor to get rid of the wait cursor.}
InitCursor;
end;
end.